package com.jiruan.invoicemanagementsystem.controller;


import com.google.gson.Gson;
import com.jiruan.invoicemanagementsystem.pojo.BaseData;
import com.jiruan.invoicemanagementsystem.pojo.Email;
import com.jiruan.invoicemanagementsystem.pojo.Order;
import com.jiruan.invoicemanagementsystem.service.IAddressService;
import com.jiruan.invoicemanagementsystem.service.IBaseDateService;
import com.jiruan.invoicemanagementsystem.service.IEmailService;
import com.jiruan.invoicemanagementsystem.service.IOrderService;
import com.jiruan.invoicemanagementsystem.service.imp.IAddressServiceImp;
import com.jiruan.invoicemanagementsystem.service.imp.IBaseDateServiceImp;
import com.jiruan.invoicemanagementsystem.service.imp.IEmailServiceImp;
import com.jiruan.invoicemanagementsystem.service.imp.IOrderServiceImp;
import com.jiruan.invoicemanagementsystem.util.PageHelper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

// 去开票页面
@WebServlet("/invoiceOrder/*")
public class InvoiceOrderController extends BaseController{

    IAddressService ias=new IAddressServiceImp();
    IBaseDateService ibds=new IBaseDateServiceImp();
    IEmailService ies=new IEmailServiceImp();

    //查询可开票的订单
    IOrderService ios=new IOrderServiceImp();
    public void queryInvoiceOrder(HttpServletRequest request, HttpServletResponse response)throws IOException {
        PageHelper ph=new PageHelper();
        int nowPage=Integer.parseInt(request.getParameter("indexpage"));
        ph.setPageIndex(nowPage);
        ph.setDateCount(ios.getCountOfCanIssuedInvoice());
        ph.setListIndex(ios.getCanIssuedInvoice(ph));

//        ArrayList<Order> orders=new ArrayList<>();
//        ArrayList<Object> o=ph.getListIndex();
//        for (Object order:o){
//            Order i=(Order) order;
//            System.out.println(i.getId());
//        }

        response.getWriter().println(new Gson().toJson(ph));

    }


    //根据订单编号 得到对应抬头信息 之后提交 ->
    public void invoiceSelect(HttpServletRequest request, HttpServletResponse response)throws IOException {
        String no=request.getParameter("no");
//        String category=request.getParameter("category");
//        String type=request.getParameter("type");
        BaseData db=ibds.queryBaseDateNo(no);

        response.getWriter().println(new Gson().toJson(db));

    }

    //获得邮箱信息   分页获得对应元素
    public void getEmail(HttpServletRequest request, HttpServletResponse response)throws IOException {

//        IEmailService ies=new IEmailServiceImp();
//        ArrayList<Email> emails=ies.queryAllEmail();
//        response.getWriter().println(new Gson().toJson(emails));
//

        PageHelper ph=new PageHelper();
        int nowPage=Integer.parseInt(request.getParameter("indexpage"));
        ph.setPageIndex(nowPage);
        ph.setDateCount(ies.queryEmailCount());
        ph.setListIndex(ies.queryEmail(ph));

//        ArrayList<Order> orders=new ArrayList<>();
//        ArrayList<Object> o=ph.getListIndex();
//        for (Object order:o){
//            Order i=(Order) order;
//            System.out.println(i.getId());
//        }

        response.getWriter().println(new Gson().toJson(ph));
    }

    //获得邮箱地址信息   分页获得对应元素
    public void getAddressPage(HttpServletRequest request, HttpServletResponse response)throws IOException {

//        IEmailService ies=new IEmailServiceImp();
//        ArrayList<Email> emails=ies.queryAllEmail();
//        response.getWriter().println(new Gson().toJson(emails));
//

        PageHelper ph=new PageHelper();
        int nowPage=Integer.parseInt(request.getParameter("indexpage"));
        ph.setPageIndex(nowPage);
        ph.setDateCount(ias.queryAddressCount());
        ph.setListIndex(ias.queryAddress(ph));

//        ArrayList<Order> orders=new ArrayList<>();
//        ArrayList<Object> o=ph.getListIndex();
//        for (Object order:o){
//            Order i=(Order) order;
//            System.out.println(i.getId());
//        }

        response.getWriter().println(new Gson().toJson(ph));
    }

    //提交开票信息
    public void  insertInvoice(HttpServletRequest request, HttpServletResponse response)throws IOException {
        String json=request.getParameter("json");
        System.out.println(json);
    }

    //接受order id
    public void receivenvoice(HttpServletRequest request, HttpServletResponse response)throws IOException {

        String id=request.getParameter("id");
        System.out.println(id);
        boolean b=ios.receivenvoice(Integer.parseInt(id));
        System.out.println(b);
        response.getWriter().println(new Gson().toJson(b));
    }








}
